package kdd.feature;

import java.sql.*;
import java.util.HashMap;

import kdd.lib.Lib;


public class ResourceAbstraction {
	Connection conn;
	
	public ResourceAbstraction(Connection connection){
		conn = connection;
	}
	
	public double getTotalPrice(String projectid) throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT SUM(item_unit_price * item_quantity) FROM resources WHERE projectid=?");
		query.setString(1, projectid);
		ResultSet result = query.executeQuery();
		if (result.next())
			return result.getDouble(1);
		Lib.assertTrue(false);
		return 0;
	}
	
	public HashMap<String, Object> getTotalPrice() throws SQLException{
		PreparedStatement query = conn.prepareStatement(
				"SELECT projectid, SUM(item_unit_price * item_quantity) FROM resources GROUP BY projectid");
		ResultSet result = query.executeQuery();
		HashMap<String, Object> ret = new HashMap<String, Object>();
		while (result.next())
			ret.put(result.getString(1), result.getDouble(2));
		System.out.println("finished getTotalPrice");
		return ret;
	}
	
}
